package br.cns;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.LineNumberReader;
import java.text.NumberFormat;

import br.cns.transformations.DegreeMatrix;

public class ConditionalProbabilityGenerator {
	private static final int NUM_NODES = 14;

	public static void main(String[] args) {
		generateConditionalDegree1();
	}

	public static void generateConditionalDegree1() {
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(2);
		nf.setMinimumFractionDigits(2);
		FileReader fr = null;
		LineNumberReader lnr = null;
		String line = null;
		Integer[][] matrix = null;
		Integer[][] degreeMatrix = null;
		double[][] p1DegreeMatrix = new double[NUM_NODES][NUM_NODES];
		double[][] countP1DegreeMatrix = new double[NUM_NODES][NUM_NODES];
		double[][] p2DegreeMatrix = new double[NUM_NODES][NUM_NODES];
		double[][] countP2DegreeMatrix = new double[NUM_NODES][NUM_NODES];
		String[] values = null;
		int value = 0;
		try {
			for (int d = 6; d <= 8; d++) {
				fr = new FileReader("C:\\Temp\\exp_2012080" + d + "\\_nsgaii_C2_M10_50_1,0000_0,0600_0.400_var.txt");
				lnr = new LineNumberReader(fr);
				line = lnr.readLine();
				while (line != null) {
					values = line.split("	");
					matrix = new Integer[NUM_NODES][NUM_NODES];
					for (int i = 0; i < matrix.length; i++) {
						for (int j = i + 1; j < matrix.length; j++) {
							value = Integer.valueOf(values[indiceVetor(j, i)]);
							if (value != 0) {
								matrix[i][j] = 1;
								matrix[j][i] = 1;
							} else {
								matrix[i][j] = 0;
								matrix[j][i] = 0;
							}
						}
					}
					line = lnr.readLine();
				}
				degreeMatrix = DegreeMatrix.getInstance().transform(matrix);
				for (int i = 0; i < matrix.length; i++) {
					for (int j = 0; j < matrix.length; j++) {
						if (i != j) {
							if (degreeMatrix[i][i] == 1) {
								if (matrix[i][j] == 1) {
									p1DegreeMatrix[i][j]++;
								}
								countP1DegreeMatrix[i][j]++;
							}
							if (degreeMatrix[j][j] == 1) {
								if (matrix[j][i] == 1) {
									p1DegreeMatrix[j][i]++;
								}
								countP1DegreeMatrix[j][i]++;
							}
							
							if (degreeMatrix[i][i] == 2) {
								if (matrix[i][j] == 1) {
									p2DegreeMatrix[i][j]++;
								}
								countP2DegreeMatrix[i][j]++;
							}
							if (degreeMatrix[j][j] == 2) {
								if (matrix[j][i] == 1) {
									p2DegreeMatrix[j][i]++;
								}
								countP2DegreeMatrix[j][i]++;
							}
						}
					}
				}
			}
			lnr.close();
			fr.close();
			StringBuffer sbP1 = new StringBuffer();
			StringBuffer sbP2 = new StringBuffer();
			FileWriter fw = new FileWriter("C:\\Temp\\probabilities_degree_1.txt");
			FileWriter fw2 = new FileWriter("C:\\Temp\\probabilities_degree_2.txt");
			for (int i = 0; i < matrix.length; i++) {
				for (int j = 0; j < matrix.length; j++) {
					if (i != j) {
						if (countP1DegreeMatrix[i][j] != 0){
							p1DegreeMatrix[i][j] /= (1.0 * countP1DegreeMatrix[i][j]);	
						}else{
							p1DegreeMatrix[i][j] = 0;
						}

						if (countP2DegreeMatrix[i][j] != 0){
							p2DegreeMatrix[i][j] /= (1.0 * countP2DegreeMatrix[i][j]);
						}else{
							p2DegreeMatrix[i][j] = 0;
						}
						
						sbP1.append(nf.format(p1DegreeMatrix[i][j]) + " ");
						sbP2.append(nf.format(p2DegreeMatrix[i][j]) + " ");	
					}else{
						sbP1.append("0,00 ");
						sbP2.append("0,00 ");
					}
					
				}
				sbP1.append("\n");
				sbP2.append("\n");
			}
			fw.write(sbP1.toString());
			fw2.write(sbP2.toString());
			fw.close();
			fw2.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	private static int indiceVetor(int j, int i) {
		return (j + (NUM_NODES - 1) * i - i * (i + 1) / 2);
	}

	public void generateConditionalDegree2() {

	}
}
